//
//  ButtonCollectionController.m
//  LNA
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 www.lanou3g.com蓝鸥. All rights reserved.
//

#import "ButtonCollectionController.h"
#import "ButtonCollectionViewCell.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "buttonModel.h"
#import "CityViewController.h"

@interface ButtonCollectionController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataArr;// 存放本页面国家中的每个城市
@end

@implementation ButtonCollectionController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    
    // 滚动方向
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
#pragma mark - collectionview 创建
    // cell 大小
    layout.itemSize = CGSizeMake(345 / 2, 540 / 3);
    // 行列间距
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    // 边界
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    // 重用池
    [_collectionView registerClass:[ButtonCollectionViewCell  class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectionView];
    [_collectionView release];
    [layout release];

    // 数据处理
    [self data];
    
    
}
#pragma mark - 数据处理
- (void)data
{
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    [sessionManager GET:self.str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"%lld",downloadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *arr = responseObject[@"data"];
        self.dataArr = [buttonModel modelHanderWithArrar:arr];
        
        [_collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
    }];
}
#pragma mark - cell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    
    return _dataArr.count;
}
#pragma mark - cell样式
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    buttonModel *m = _dataArr[indexPath.row];
    ButtonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.cnname.text = m.catename;
    cell.cnname.font = [UIFont systemFontOfSize:14];
    cell.cnname.textColor = [UIColor whiteColor];
    cell.enname.text = m.catename_en;
    cell.enname.font = [UIFont systemFontOfSize:14];
    cell.enname.textColor = [UIColor whiteColor];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.photo]];
    cell.beenstrLabel.text = m.beenstr;
    cell.beenstrLabel.textColor = [UIColor colorWithRed:137 /255.0 green:137 / 255.0 blue:137 /255.0 alpha:1];
    cell.beenstrLabel.font = [UIFont systemFontOfSize:14];
    cell.representativeLabel.text = m.representative;
    cell.representativeLabel.numberOfLines = 0;
    cell.representativeLabel.font = [UIFont systemFontOfSize:14];
    cell.representativeLabel.textColor = [UIColor colorWithRed:112 / 255.0 green:112 / 255.0 blue:112 / 255.0 alpha:1];

    return cell;
    
    
}
#pragma mark - cell点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    buttonModel *m = _dataArr[indexPath.row];
    CityViewController *cityVC = [[CityViewController alloc] init];
    
    cityVC.str = m.cid;
    
    [self.navigationController pushViewController:cityVC animated:YES];
    
    [cityVC release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
